package com.mukefile.service.impl;

import com.mukefile.dao.MukeFileDao;
import com.mukefile.pojo.MukeFile;
import com.mukefile.pojo.User;
import com.mukefile.service.MukeFileService;
import com.mukefile.utils.GetPathProperties;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Author: kenewstar
 * @Description:
 * @Date:Created in 2020/5/6
 */
@Service("mukeFileService")
@Transactional(rollbackFor = Exception.class)
public class MukeFileServiceImpl implements MukeFileService {
    @Autowired
    private MukeFileDao mukeFileDao;
    @Autowired
    private GetPathProperties getPathProperties;

    private Logger logger = Logger.getLogger(MukeFileServiceImpl.class);

    @Override
    public int fileUpload(File file, MukeFile mukeFile) {
        //日志
        String username = mukeFile.getUser().getUsername();
        this.logger.info("用户："+username+"发起了上传文件操作！");

        int i = this.mukeFileDao.fileUpload(file, mukeFile);
        String path = getPathProperties.getDiskRootPath()+File.separator;
        if (i==0){
            //失败
            this.logger.info("用户："+username+"上传文件"+mukeFile.getFileName()+"失败！");
            return 0;
        }else {
            //成功
            try {
            //上传文件
            FileUtils.copyFile(file,
                    new File(path+mukeFile.getUser().getDiskRootPath()
                            +File.separator+ "DiskA"+File.separator
                            +mukeFile.getFileName()));
                this.logger.info("用户："+username+"上传文件"+mukeFile.getFileName()+"成功！");
            } catch (IOException e) {
                this.logger.info("用户："+username+"上传文件"+mukeFile.getFileName()+"失败！");
                e.printStackTrace();
            }
        }
        return 1;
    }

    @Override
    public List<MukeFile> findAllFile(User user) {
        this.logger.info("用户："+user.getUsername()+",查询了所有文件！");
        List<MukeFile> files = this.mukeFileDao.selectAllFile(user);
        return files;
    }

    @Override
    public int deleteFile(MukeFile mukeFile) {
        //获取用户名
        String username = mukeFile.getUser().getUsername();
        this.logger.info("用户："+username+",对文件"+mukeFile.getFileName()+" 进行了删除！");

        int i = this.mukeFileDao.deleteFile(mukeFile);
        String path = getPathProperties.getDiskRootPath()+File.separator;
        if (i==0){
            //删除失败
            this.logger.info("用户："+username+",删除文件"+mukeFile.getFileName()+" 失败！");
            return 0;
        }else {
            String absPath = path+mukeFile.getUser().getDiskRootPath()
                    +File.separator+ "DiskA"+File.separator
                    +mukeFile.getFileName();
            File file = new File(absPath);
            if (file.exists()){
                //存在
                file.delete();
            }
            this.logger.info("用户："+username+",删除文件"+mukeFile.getFileName()+" 成功！");
        }
        return 1;
    }

    @Override
    public void download(User user,String filename) {
        //用户下载文件
        this.logger.info("用户："+user.getUsername()+",下载了文件："+filename);
    }
}
